<!DOCTYPE html>
<html>
<head>
<meta name="renderer" content="webkit"/>
<meta name="force-rendering" content="webkit"/>
<meta http-equiv="Cache-Control" content="max-age=7200" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
</head>
<body>
	<button type="button" class="layui-btn" id="test1">
	  <i class="layui-icon">&#xe67c;</i>上传头像
	</button>
    <div id="headImgDiv">
    </div>

	<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../js/jq.js"></script>
	<script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript">
	initData();
	
	function initData(){
		$.ajax({
			type : 'get',
			url : '/user/current',
			async : false,
			success : function(data) {
				var url = data.headImgUrl;
				if(url!=null && url!=""){
                    var a = "<a target='_blank' href='" + url +"'><img src='" + url + "'></img></a>";
					$("#headImgDiv").html(a);
				}
			}
		});
	}
	
	layui.use(['upload','layer'], function(){
		var layer = layui.layer;
		var upload = layui.upload;
		upload.render({
			elem: '#test1', //绑定元素
			accept: 'images', //允许上传的文件类型
			url: '/upload', //上传接口
			done: function(res, index, upload){
		    	if(res){
		    		 var url = res.message;
					$.ajax({
						type : 'put',
						url : '/user?headImgUrl='+url,
						async : false,
						success : function(data) {
							layer.msg("上传成功");
					        var a = "<a target='_blank' href='" + url +"'><img src='" + url + "'></img></a>";
					        $("#headImgDiv").html(a);
					        var img = parent.$(".admin-header-user img");
							img.attr("src", url);
							}
						});
	    		} else {
	    			layer.msg("上传失败");
	    		}
    		},
    		error: function(e){
    			console.log(e)	
    		}
  		});
	});
	
	</script>
</body>
</html>